package com.hankol.newretail

import android.view.View
import android.widget.LinearLayout
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.MessageBus
import com.baselibrary.adapter.MyOrderTabAdapter
import com.baselibrary.bean.MyOrderTabBean
import com.baselibrary.router.ActivityRouter
import com.baselibrary.activity.BaseActivity
import com.hankol.newretail.adapter.MyEvaluationTabVp
import com.hankol.newretail.adapter.order.MyEvaluationTabAdapter
import com.hankol.newretail.databinding.ActivityEvaluationCenterBinding
import com.hankol.newretail.fragment.MyEvaluationFragment
import com.hankol.newretail.viewmodel.OrderAboutViewModel
import com.transitionseverywhere.TransitionManager
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 *@author:hanke
 *2022/9/5
 *desc:
 */
@Route(path = ActivityRouter.NEW_RETAIL.EVALUATION_CENTER)
class EvaluationCenterActivity : BaseActivity() {
    private val mMyOrderTabAdapter: MyEvaluationTabAdapter by lazy { MyEvaluationTabAdapter() }
    private val mMyOrderTabVp: MyEvaluationTabVp by lazy { MyEvaluationTabVp(supportFragmentManager) }
    private lateinit var dataBinding: ActivityEvaluationCenterBinding
    private lateinit var viewModel: OrderAboutViewModel
    private var tabList: ArrayList<MyOrderTabBean> = arrayListOf(MyOrderTabBean("待评价", true), MyOrderTabBean("已评价", false))

    private var fragmentList: ArrayList<MyEvaluationFragment> = arrayListOf(//类型：0待评价，1已评价
            MyEvaluationFragment.newInstance(0), MyEvaluationFragment.newInstance(1))

    override fun getLayoutId(): Int {
        initImmersion(true)
        return R.layout.activity_evaluation_center
    }

    override fun init() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this)
        }
        setStatusBarHeight(R.color.color_F9F9F9)
        dataBinding = getDataBinding<ActivityEvaluationCenterBinding>()
        viewModel = getActivityViewModelProvider(this).get(OrderAboutViewModel::class.java)
        dataBinding.ivBack.setOnClickListener { finish() }
        dataBinding.rvTabEvaluation.adapter = mMyOrderTabAdapter
        dataBinding.vpEvaluation.adapter = mMyOrderTabVp
        mMyOrderTabVp.setDatas(fragmentList)
        mMyOrderTabAdapter.setNewData(tabList)
        dataBinding.vpEvaluation.offscreenPageLimit = fragmentList.size
        loadData()
    }

    fun loadData() {

        mMyOrderTabAdapter.setOnLoadItemViewCallBack(object : MyEvaluationTabAdapter.OnLoadItemViewCallBack {

            override fun onSelectedDrawComplete(view: View, position: Int) {
                view.post {
                    if (dataBinding.vpEvaluation.currentItem != position) {
                        dataBinding.vpEvaluation.setCurrentItem(position, true)
                    }
                    TransitionManager.beginDelayedTransition(dataBinding.llTransitionManager)
                    val params: LinearLayout.LayoutParams = dataBinding.ivOrderTabIcon.layoutParams as LinearLayout.LayoutParams
                    params.setMargins(view.left + view.width / 2 + dataBinding.ivOrderTabIcon.width / 2, view.top, 0, 0)
                    dataBinding.ivOrderTabIcon.layoutParams = params
                }
            }

        })
        mMyOrderTabAdapter.setOnItemClickListener { adapter, view, position ->
            mMyOrderTabAdapter.upSelectedTab(position)
        }

        dataBinding.vpEvaluation.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                mMyOrderTabAdapter.upSelectedTab(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
            }

        })
        dataBinding.vpEvaluation.currentItem = 0
        mMyOrderTabAdapter.upSelectedTab(0)
    }

    //EventBus 接受方法
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEvent(messageBus: MessageBus) {
        if (messageBus.codeType == MessageBus.msgId_refresh_evaluation_number) {
            var number = messageBus.param1 as Int
            tabList[0].tabName = if (number == 0) {
                "待评价"
            } else "待评价 $number"
            mMyOrderTabAdapter.notifyDataSetChanged()
        } else if (messageBus.codeType == MessageBus.msgId_refresh_evaluation_number_have) {
            var number = messageBus.param1 as Int
            tabList[1].tabName = if (number == 0) {
                "已评价"
            } else "已评价 $number"
            dataBinding.rvTabEvaluation.postDelayed(object :Runnable{
                override fun run() {
                    mMyOrderTabAdapter.notifyDataSetChanged()
                }
            },500)
        } else if (messageBus.codeType == MessageBus.msgId_refresh_evaluation_list) {
            dataBinding.vpEvaluation.currentItem = 1
            mMyOrderTabAdapter.upSelectedTab(1)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this)
        }
    }
}